
package com.yimixian.app.adapter;

import com.yimixian.app.R;
import com.yimixian.app.address.ChooseCityActivity;
import com.yimixian.app.model.City;
import com.yimixian.app.model.Region;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ChooseCityAdapter extends ArrayListAdapter<City> {

    public ChooseCityAdapter(Activity context) {
        super(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = mContext.getLayoutInflater();
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.choose_city_list_item, null);
            viewHolder.cityNameText = (TextView) convertView.findViewById(R.id.city_name_text);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.cityNameText.setText(mList.get(position).name);
        viewHolder.cityNameText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String mRegionId = mList.get(position).id;
                String mCityName = mList.get(position).name;
                if (mContext instanceof ChooseCityActivity) {
                    ((ChooseCityActivity) mContext).returnWithCityName(mRegionId, mCityName);
                }
            }
        });
        return convertView;
    }

    public final class ViewHolder {
        public TextView cityNameText;
    }

}
